import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;

/*
 */
public class CardDemo {
    //花色
    public static String[] Suit = {"♦","♣","♥","♠"};

    //使用顺序表实现一副牌（不包含大小王）
    public List<Card> buyDeck(){
        List<Card> deck = new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {//花色
            for (int j = 1; j <= 13; j++) {//牌值
                String suit = Suit[i];
                int rank = j;
                Card card = new Card();
                card.suit = suit;
                card.rank = rank;

                deck.add(card);
            }
        }
        return deck;
    }

    //打乱顺序:生成随机坐标，与指定坐标交换
    private static void swap(List<Card> deck,int i,int j ){
        Card card = deck.get(i);
        deck.set(i,deck.get(j));
        deck.set(j,card);

    }
    private static List<Card> shuffle(List<Card> deck){
        Random random = new Random();
        int j = random.nextInt(52);//0 - 51

        for (int i = deck.size() - 1; i >= 0 ; i--) {
             swap(deck,i,j);
        }
        return deck;
    }

    public static void main(String[] args) {
    CardDemo cardDemo = new CardDemo();

    List<Card> deck = cardDemo.buyDeck();

    System.out.println(deck);//洗牌前

    deck = CardDemo.shuffle(deck);

    System.out.println(deck);//洗牌后

    List<List<Card>> hand = new ArrayList<>();

    hand.add(new ArrayList<>());
    hand.add(new ArrayList<>());
    hand.add(new ArrayList<>());

    //发牌:一共发5轮，一轮每人发一张
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 3; j++) {
            hand.get(j).add(deck.remove(0));
        }
    }
    //第一个人的牌
    System.out.println(hand.get(0));
    //第二个人的牌
    System.out.println(hand.get(1));
    //第三个人的牌
    System.out.println(hand.get(2));

    //剩余的牌
    System.out.println(deck);
}

}
